package diamondRingServlet;

import diamondRingEntity.User;
import diamondRingEntity.DiamondRingUser;
import diamondRingService.impl.UserServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
//登录查询
@WebServlet("/LoginServlet")
public class LoginServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=utf-8");
        response.setCharacterEncoding("utf-8");
        PrintWriter out=response.getWriter();
        UserServiceImpl adm=new UserServiceImpl();
        String email=null;
        email=request.getParameter("email");
        String password="0";
        password=request.getParameter("password");
        User admin=new User(email,password);
        int count = adm.Login(admin);
        DiamondRingUser user=adm.findUser(email,Integer.parseInt(password));
        HttpSession session= request.getSession();
        if(count>0)
        {
            session.setAttribute("name",user.getLoginName());
            session.setAttribute("id",user.getUserId());
        }
        out.print(count);
        out.flush();
        out.close();
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request,response);
    }
}
